﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using WoWArmoryParser;
using Microsoft.Win32;

namespace WoWGRA
{
    /// <summary>
    /// Interaction logic for MainRaider.xaml
    /// </summary>
    public partial class MainRaider : Window
    {
        public MainRaider()
        {
            InitializeComponent();
            this.comboBox1.ItemsSource = Enum.GetNames(typeof(RegionEnum));

        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openGuild = new OpenFileDialog();
            openGuild.ShowDialog(this);
            string guildfilename;
            guildfilename = openGuild.FileName.ToString();
            LoadRaider(guildfilename);
        }
        
        public void LoadRaider(string openguild)
        {
            //int x = 0;
            DataSet GuildRoster = new DataSet("GuildRoster");
            GuildRoster.ReadXml(openguild);

            foreach (DataRow r in GuildRoster.Tables[1].Rows)
            {
                string member = r["MainRaider"].ToString();
                if (member == "1")
                {
                    listBox2.Items.Add(r["Members"]);
                }
                else
                {
                    listBox1.Items.Add(r["Members"]);
                }
                
            }
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //do while to stop from adding multiple that are already there
            listBox2.Items.Add(listBox1.SelectedItem);
            listBox1.Items.Remove(listBox1.SelectedItem);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            listBox1.Items.Add(listBox2.SelectedItem);
            listBox2.Items.Remove(listBox2.SelectedItem);
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            int x = 0;
            int d = 0;
            DataSet GuildRoster = new DataSet("GuildRoster");
            GuildRoster.ReadXml("GuildRoster.xml");
           
            while (x < listBox2.Items.Count)
            {
                string currentitem = listBox2.Items[x].ToString();
                foreach (DataRow r in GuildRoster.Tables[1].Rows)
                {
                    if (currentitem == r["Members"].ToString())
                    {
                        r["MainRaider"] = 1;
                    }
                }
                x++;
            }

            while (d < listBox1.Items.Count)
            {
                string currentitem2 = listBox1.Items[d].ToString();
                foreach (DataRow r in GuildRoster.Tables[1].Rows)
                {
                    if (currentitem2 == r["Members"].ToString())
                    {
                        r["MainRaider"] = 0;
                    }
                }
                d++;
            }
            

            GuildRoster.WriteXml("GuildRoster.xml");
            this.Close();
            
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            string region = comboBox1.SelectedValue.ToString();
            
            
            if (region == "USA")
            {
                GuildInfo mGuild = ArmoryParser.GetGuild(RegionEnum.USA, textBox2.Text.ToString(), textBox3.Text.ToString());
                fillmembers(mGuild);
            }
            else if (region == "Europe")
            {
                GuildInfo mGuild = ArmoryParser.GetGuild(RegionEnum.Europe, textBox2.Text.ToString(), textBox3.Text.ToString());
            }
            else if (region == "Korea")
            {
                GuildInfo mGuild = ArmoryParser.GetGuild(RegionEnum.Korea, textBox2.Text.ToString(), textBox3.Text.ToString());
            }
            else if (region == "Taiwan")
            {
                GuildInfo mGuild = ArmoryParser.GetGuild(RegionEnum.Taiwan, textBox2.Text.ToString(), textBox3.Text.ToString());
            }
            else if (region == "China")
            {
                GuildInfo mGuild = ArmoryParser.GetGuild(RegionEnum.China, textBox2.Text.ToString(), textBox3.Text.ToString());
            }
            
                        
            //GuildInfo mGuild = ArmoryParser.GetGuild(RegionEnum.USA, "Cenarius", "Wrought from Ruin");
        }

        public void fillmembers(GuildInfo mGuild)
        {
            //int membercount = mGuild.MembersCount;
            int count = 0;
                        
            GuildMemberInfo[] x = mGuild.Members.ToArray();
            int membercount = x.Length;
            while (count < membercount)
            {
                listBox1.Items.Add(x[count].Name.ToString());
                count++;
            }
        }

        
    }
}
